﻿//------------------------------------------------------------------------------------------------- 
// <copyright file="FormGeneral.cs" company="Elpaulo Productions">
//   Copyright (c) Elpaulo Productions. All rights reserved.
// </copyright>
//-------------------------------------------------------------------------------------------------
namespace Elpaulo.Canardpc.CanardpcImageUploader
{
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    using Elpaulo.Canardpc.TofCanardpcAPI;

    /// <summary>
    /// General form of the application
    /// </summary>
    public partial class FormGeneral : Form
    {
        /// <summary>
        /// Initializes a new instance of the FormGeneral class
        /// </summary>
        public FormGeneral()
        {
            InitializeComponent();

            CheckForIllegalCrossThreadCalls = false;

         
            // ImagePanel panel = new ImagePanel("d:\\9e2af697fd4df427bb8560381a876c00aitd.jpg");
            // flpImagesToUpload.Controls.Add(panel);

             // NameValueCollection nvc = new NameValueCollection();
             // nvc.Add(Properties.Settings.Default.SubmitFieldName, "");

             // string retour = TofCanardpcAPI.UploadImage(Properties.Settings.Default.URLTofCanarpcCom, "d:\\9e2af697fd4df427bb8560381a876c00aitd.jpg", Properties.Settings.Default.FileFieldName, "image/jpeg", nvc);

             // Console.WriteLine(retour);
        }

        /// <summary>
        /// Checks if the drag contains files
        /// </summary>
        /// <param name="sender">The adding label</param>
        /// <param name="e">Event args</param>
        private void LblDragHere_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Move;
            }
        }

        /// <summary>
        /// Add ImagePanels for each images found in the drag
        /// </summary>
        /// <param name="sender">The adding label</param>
        /// <param name="e">Event args</param>
        private void LblDragHere_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] files = e.Data.GetData(DataFormats.FileDrop) as string[];

                // filtrer on aviable extensions             
                files = files.Where(file => Properties.Settings.Default.AviableExtensions.Contains(Path.GetExtension(file).ToUpper())).ToArray();

                foreach (string image in files)
                {
                    ImagePanel imagePanel = new ImagePanel(image);
                    imagePanel.Deleted += new DeletedItem(ImagePanel_Deleted);
                    flpImagesToUpload.Controls.Add(imagePanel);
                }
            }
        }

        /// <summary>
        /// Removes the imagepanel from the flowlayoutpanel
        /// </summary>
        /// <param name="sender">ImagePanel to remove</param>
        /// <param name="e">Event args</param>
        private void ImagePanel_Deleted(object sender, EventArgs e)
        {
            flpImagesToUpload.Controls.Remove(sender as ImagePanel);
        }

        /// <summary>
        /// Start the upload of ImagePanels witch state is NotUploaded
        /// </summary>
        /// <param name="sender">Start button</param>
        /// <param name="e">Event args</param>
        private void BtnStartUpload_Click(object sender, EventArgs e)
        {
            List<ImagePanel> imagePanels = new List<ImagePanel>();

            foreach (Control control in flpImagesToUpload.Controls)
            {
                if (control is ImagePanel)
                {
                    imagePanels.Add(control as ImagePanel);
                }
            }

            UploadWorker uploadWorker = new UploadWorker(imagePanels.Where(imagePanel => imagePanel.ImageState == ImageState.NotUploaded).ToList());
            uploadWorker.RunWorkerAsync();
        }

        /// <summary>
        /// Click event on the exit menu button
        /// </summary>
        /// <param name="sender">Exit menu button</param>
        /// <param name="e">Event args</param>
        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
